package com.small.nacos.config.server.service.dump;

import small.common.crypto.util.KeyUtil;
import com.small.nacos.config.server.service.dump.processor.DumpProcessor;
import com.small.nacos.config.server.service.dump.task.DumpTask;
import com.small.nacos.config.server.service.dump.task.TaskManager;
import com.small.nacos.config.server.service.responsitory.PersistService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class DumpService {

    protected static final Logger LOGGER = LoggerFactory.getLogger(DumpService.class);

    protected DumpProcessor processor;

    private TaskManager dumpTaskMgr;

    protected final PersistService persistService;

    public DumpService(PersistService persistService) {
        this.persistService = persistService;
        this.processor = new DumpProcessor(this);
        this.dumpTaskMgr = new TaskManager("com.small.nacos.server.DumpTaskManager");
        dumpTaskMgr.setDefaultTaskProcessor(processor);
    }

    public void dump(String dataId, String group, String tenant, long lastModified) {
        String groupKey = KeyUtil.getKey(dataId, group, tenant);
        String taskKey = String.join("+", dataId, group, tenant);
        dumpTaskMgr.addTask(taskKey, new DumpTask(groupKey, lastModified));
        LOGGER.info("[dump-task] add task. groupKey={}, taskKey={}", groupKey, taskKey);
    }

    public PersistService getPersistService() {
        return persistService;
    }
}
